<template>
  <div class="PrintRedDialog">
    <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> 
      <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
    <el-dialog title="发票冲红" :visible.sync="$parent.PrintRedFlag" width="30%" v-dialogDrag border @close="closeDialog()">
      <el-form :model="printRedData"   :inline="true" label-width="120px" ref="printRed" :rules="printRedRules"  :disabled="printRedFlag">
        <el-row :gutter="5">
          <el-form-item label="冲红发票代码"  prop="invoiceRedItem.invoiceCode" >
            <el-input v-model="printRedData.invoiceRedItem.invoiceCode" :maxlength="20"></el-input>
          </el-form-item>
          <el-form-item label="冲红发票号:" prop="invoiceRedItem.invoiceNo" >
            <el-input v-model="printRedData.invoiceRedItem.invoiceNo" :maxlength="20"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="冲红发票抬头"   prop="invoiceRedItem.invoiceTitle" >
            <el-input v-model="printRedData.invoiceRedItem.invoiceTitle"  :maxlength="100" class="w425"></el-input>
          </el-form-item>

        </el-row>
        <el-row :gutter="5">
          <el-form-item label="冲红发票金额:" >
            <el-input v-model="changeRedCharge" :disabled="true" :maxlength="20"></el-input>
          </el-form-item>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary"  @click="PrintRedDataMethod()">打印发票</el-button>

      </div>
    </el-dialog>
  </div>
</template>
<script>
  import * as Type from '@/api/service'
  import {getLodop} from '@/utils/lodopFuncs'
  export default {
    name: "PrintRedDialog",
    props: {
      PrintInvoiceFlag: {
        type: Boolean
      },
      selectedRed: {
        type: Array,
        require: true
      },
      businessCode: {
        type: String,
        default: ""
      }
    },
    data() {
      return {
          printRedData:{
            invoiceRedItem:{
            'invoiceCode':'',//冲红发票代码
            'invoiceNo':'',//冲红发票号码
            'invoiceTitle':'',//冲红发票抬头
            'invoiceCharge':'',//冲红发票金额
            'remark':''//备注
          },
          'businessCode':this.businessCode,
            invoiceId:{//结算主键集合
              'id':'',
              'version':''
            },
        },
        printRedFlag:false,//打印标志
        printRedRules: {
           'invoiceRedItem.invoiceCode': [
          {required: true, message: '请输入发票代码', trigger: 'blur'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
        ], 'invoiceRedItem.invoiceNo': [
          {required: true, message: '请输入发票号', trigger: 'blur'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
        ], 'invoiceRedItem.invoiceTitle': [
          {required: true, message: '请输入发票抬头', trigger: 'change'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
        ],
      },
      }
    },
    created() {
      // this.loadChargeInvoiceAllInfo()
    },
    mounted() {


    },
    computed:{
      //计算冲红发票金额
      changeRedCharge(){
        //定义总合计
        let settleTotal=0;
        let selectData=this.selectedRed
        if(selectData && selectData.length>0) {
          let selectItem = null
          let settleCharge = 0;
          for (let i = 0; i < selectData.length; i++) {
            selectItem = selectData[i]
            //发票金额转换为数值
            settleCharge = new Number(selectItem.invoiceCharge)
            settleTotal += settleCharge

          }   //返回合计金额
          settleTotal= Number(settleTotal).toFixed(2)
          return settleTotal;
        }
      },
    },
    watch: {
      '$parent.PrintInvoiceFlag'() {
        if (this.$parent.PrintInvoiceFlag == false) {
          //清空所有预加载项
        }else{
        }
      }
    },
    methods: {
      /*测试*/
      PrintRedDataMethod:function(){
        let this_print=this;
        //业务编码非空
        if(this.businessCode!=null && this.businessCode!="") {
          this.printRedData.businessCode = this.businessCode
          //获得传过来的发票数据id和version
          let selectData = this.selectedRed
          //计算结果赋值给发票金额
          if (selectData && selectData.length == 1) {
            this.printRedData.invoiceId={
              'id':selectData[0].id,
              'version':selectData[0].version
            }
            this.printRedData.invoiceRedItem.invoiceCharge=this.changeRedCharge

          }
          //定义发票打印数据
          let postData = this_print.printRedData
          //判断查询条件报错
          if(this.businessCode!=null && this.businessCode!=""){
              Type['saveChargeInvoiceToRed'](postData).then(response => {
              let result = response;
              if (result){
                //"冲红发票打印成功"
              this_print.PrintRedInvoiceMethod(result.data);
              //关闭
              this_print.$parent.PrintRedFlag = false
              }
            }).catch(error => {
                this_print.$message(error)

              })
          }

        }

      }, //打印红票
      PrintRedInvoiceMethod(printData){
        let LODOP =  getLodop();
        if (LODOP == null || !LODOP) {
          this.$message('您的浏览器不支持打印控件，请用IE重试或安装打印控件后重试')
          return;
        }
        //冲红字
        let redWord="冲红";//数值是负的
        //业务编码
        let businessCode=printData.businessCode;
        //交款人
        let persion="交款人";
        //项目名称
        let itemName="项目名称";
        let itemPrice="XXXX";
        let itemTotal="XXXX";
        let bigTitle="壹贰叁肆伍陆柒捌玖";
        //收费日期
        let  businessTime="2020   05   22"
        //业务办理人
        let  businessPersion="管理员";
        //设置纸张
        //LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");
        //设置字体
        LODOP.SET_PRINT_STYLE("FontSize",11);
        //调用打印控件进行测试
        LODOP.ADD_PRINT_TEXT(20,50,150,50, redWord);
        LODOP.ADD_PRINT_TEXT(39,50,150,50, businessCode);
        LODOP.ADD_PRINT_TEXT(39,250,150,50, businessTime);
        LODOP.ADD_PRINT_TEXT(60,50,150,50, persion);
        LODOP.ADD_PRINT_TEXT(80,50,150,50, itemName);
        LODOP.ADD_PRINT_TEXT(80,120,150,50, itemPrice);
        LODOP.ADD_PRINT_TEXT(300,220,150,50, itemTotal);
        LODOP.ADD_PRINT_TEXT(300,420,150,50, bigTitle);
        LODOP.ADD_PRINT_TEXT(340,420,150,50, businessPersion);
        //打印预览
        LODOP.PREVIEW();
        //直接打印
        //LODOP.PRINT();
        //LODOP.PRINT_DESIGN();
      }, closeDialog() {
        this.$parent.PrintInvoiceFlag = false
      },
    }
  }
</script>

<style scoped>

</style>
